﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Button = System.Windows.Controls.Button;

namespace weighing
{
    /// <summary>
    /// PlateHorizitolInput.xaml 的交互逻辑
    /// </summary>
    public partial class PlateHorizitolInput : System.Windows.Controls.UserControl
    {
        #region var
        private string currProvince = string.Empty;//省份
        private string currLate = String.Empty;//字母部份
        private string currOther = String.Empty;//其它部分
        private string PlageResult = String.Empty;//其它部分

        #endregion

        public Action<string,bool> Input { get; set; }
        /// <summary>
        /// 默认传入的车牌号，修改时可以用到
        /// </summary>
        public string CarNumber { get; set; }

        public PlateHorizitolInput()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ClearText();
        }

        public void ClearText()
        {
            this.currProvince = "";
            this.currLate = "";
            this.currOther = "";
            this.PlageResult = "";
        }

        private void BuildNumber(string inputStr)
        {
            string res = currProvince + currLate + currOther;
            this.PlageResult = res;
            if (MyHelper.RegexHelper.IsVehicleNumber(PlageResult))
            {
                this.Input?.Invoke(PlageResult, true);
            }
            else
            {
                this.Input?.Invoke(inputStr, false);
            } 
        }

        private void ProviceBtn_click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            currProvince = btn.Content.ToString();
            BuildNumber(currProvince);
        }

        private void LaterBtn_click(object sender, RoutedEventArgs e)
        {
            Button? btn = sender as Button;
            BuildNumber(btn?.Content.ToString());
            if (currLate.IsNullOrEmpty())
            {
                currLate = btn.Content.ToString();               
            }
            else
            {
                if (currOther.Length >= 6)
                {
                    string msg = "已超出限定长度";
                    CommonFunction.AlertInfo(msg);
                    return;
                }
                currOther += btn.Content.ToString();              
            }    
            
        }

        private void NumberBtn_click(object sender, RoutedEventArgs e)
        {
            if (currOther.Length >= 6)
            {
                string msg = "已超出限定长度";
                CommonFunction.AlertInfo(msg);
                return;
            }
            Button btn = sender as Button;
            currOther += btn.Content.ToString();
            BuildNumber(currOther);
        }
    }
}
